package com.java.service.impl;


import java.util.List;

import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.HibernateTransactionManager;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

import com.java.dao.DeptDao;
import com.java.service.IDeptService;
import com.training.pojo.chengshu.UDept;
import com.training.pojo.chengshu.UUser;

public class DeptServiceImpl  extends HibernateDaoSupport implements IDeptService {

	private DeptDao deptDao;
	public List<UDept> deptList(int start) {
		//System.out.println("deptlist------------");
		List<UDept> deptlist = deptDao.deptList(start);
		
		return deptlist;
	}
	public Integer countDept() {
		int n = deptDao.countDept();
		//System.out.println("countdeptn;;;;;");
		return n;
	}
	public DeptDao getDeptDao() {
		return deptDao;
	}
	public void setDeptDao(DeptDao deptDao) {
		this.deptDao = deptDao;
	}
	public List<UUser> empList(int deptid) {
	    List<UUser> list = deptDao.empList(deptid);
	   // System.out.println(list.size());
		return list;
	}
	public Integer countUUser(int deptid) {
		int n = deptDao.countUUser(deptid);
		//System.out.println("uuuuuuuuuu"+n);
		return n;
	}
	public List<UUser> userList() {
		List<UUser> list = deptDao.userList();
		//System.out.println("uuuuuuuuuser"+list.size());
		return list;
	}
	public boolean addDept(UDept d) {
		boolean b = deptDao.addDept(d);
		//System.out.println(b+"dddddddddddd");
		return b;
	}
	public UDept getDept(int deptid) {
		UDept d = deptDao.getDept(deptid);
		//System.out.println(deptid+"+++++");
		return d;
	}
	public UDept updateDept(UDept d) {
		UDept dd = deptDao.updateDept(d);
		//System.out.println(dd.getDeptname()+"+++updatedept");
		return dd;
	}
	public boolean deleteDept(final int deptid) {
		
			//System.out.println("deleteDeptService");
			
			HibernateTransactionManager htm = new HibernateTransactionManager(this.getSessionFactory());
		
			TransactionTemplate tt = new TransactionTemplate(htm);
			tt.execute(new TransactionCallback<Object>() {
				public Object doInTransaction(TransactionStatus arg0) {
				
					HibernateTemplate hibtemp = getHibernateTemplate();
						deptDao.updateMotor(deptid);
						deptDao.updateUser(deptid);
						deptDao.deleDept(deptid);
						
						//System.out.println("deleteDeptService TransactionTemplate");
					return null;
				}
				
			});
			//System.out.println("deleteDeptService return");
			return true;
	}
	public List<UDept> select(UDept d) {
		List<UDept> list = deptDao.select(d);
		System.out.println(list.size()+"serviceccccccccc");
		return list;
	}
	public List<UUser> list() {
		List<UUser> list = deptDao.list();
		System.out.println("listsssssssssss"+list.size());
		return list;
	}
	public List<UDept> list1() {
		List<UDept> list = deptDao.list1();
		System.out.println("list11111111111"+list.size());
		return list;
	}
	
	
}
